package com.example.demo.controller;


import com.example.demo.entity.LikeNoteComment;
import com.example.demo.entity.NoteCollection;
import com.example.demo.entity.NoteComment;
import com.example.demo.service.NoteCollectionService;
import com.example.demo.vo.ViewNoteCollectionInfo;
import com.example.demo.vo.ViewNoteCreateInfo;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("v1")
public class NoteCollectionController {

    @Autowired
    private NoteCollectionService noteCollectionService;

    //根据用户ID查找对应用户喜欢的笔记
    @GetMapping("/NoteCollectioninfo")
    public PageInfo<ViewNoteCollectionInfo> getPageNoteCollectionInfo(
                                                                       @RequestParam Integer user_id) {
        PageInfo<ViewNoteCollectionInfo> pageInfo = noteCollectionService.findNoteCollectionInfo(user_id);
        return pageInfo;
    }

    //取消收藏
    @DeleteMapping("/NoteCollection/{id}")
    public Integer deleteNoteCollection(@PathVariable("id") Integer collection_id) {
        return noteCollectionService.deleteNoteCollection(collection_id);
    }

    //根据用户id统计收藏数量
    @GetMapping("/NoteCollection/count")
    public ResponseEntity<Integer> countcollection(
            @RequestParam Integer user_id) {
        Integer count = noteCollectionService.countcollection(user_id);
        return ResponseEntity.ok(count);
    }

    //根据笔记id统计收藏数量
        @GetMapping("/NoteCollectionnoteid/count")
    public ResponseEntity<Integer> countcollectionnoteid(
            @RequestParam Integer note_create_id) {
        Integer count = noteCollectionService.countcollectionnoteid(note_create_id);
        return ResponseEntity.ok(count);
    }

    //根据用户id和笔记id，收藏笔记。
    @PostMapping("/NoteCollectionadd")
    public Map<String, Object> insertNoteCollection(@RequestBody NoteCollection noteCollection) {
        boolean success = noteCollectionService.insertNoteCollection(noteCollection);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);  //响应的success字段
        response.put("message", success ? "收藏成功" : "收藏失败");
        return response;
    }

    // 取消收藏（笔记详情页）
    @DeleteMapping("/NoteCollectiondelete")
    public Map<String, Object> deleteNoteCollection1(@RequestBody NoteCollection noteCollection) {
        boolean success = noteCollectionService.deleteNoteCollection1(noteCollection);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "取消收藏成功" : "取消收藏失败");
        return response;
    }

    //检查是否已经收藏
    @GetMapping("/NoteCollectioncheck")
    public Map<String, Object> checkIfNoteCollection(@RequestParam Integer user_id, @RequestParam Integer note_create_id) {
        boolean collected = noteCollectionService.checkIfNoteCollection(user_id, note_create_id);
        Map<String, Object> response = new HashMap<>();
        response.put("collected", collected);
        return response;
        //创建了一个HashMap实例，它实现了Map接口，用于存储键值对。response变量被用作存储HTTP响应数据的容器，
        //其中键是String类型的，而值是Object类型的，这意味着可以在其中存储任何类型的对象。
    }


}
